#include "stu_interface.h"
#include "ui_stu_interface.h"
#include "bookstore.h"
#include "book.h"
#include "utils.h"
#include "student.h"
#include "init.h"
#include <vector>
#include <QMessageBox>
#include<algorithm>

using namespace bookandrecord;
using namespace bookstore;
using namespace utils;
using namespace student;



stu_interface::stu_interface(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::stu_interface) {
    ui->setupUi(this);
    page = 0;//当前页数
    pagesize = 0;//总页数
    page_size = 3;//一页放几本书
    MenuToBookUI = new MenuToBooks(this);
    MenuToBookUI->hide();
    changeUI = new changeCode(this);
    changeUI->hide();
    addMessageUI = new add_Message(this);
    addMessageUI->hide();
    messageListUI = new show_messageList(this);
    messageListUI->hide();
    connect(this, SIGNAL(sendData(Menu*, Student*)), MenuToBookUI, SLOT(getData(Menu*, Student*)));
    connect(this, SIGNAL(sendData(Student*)), changeUI, SLOT(getData(Student*)));
    connect(this, SIGNAL(sendData(Student*,int)), addMessageUI, SLOT(getData(Student*,int)));
    connect(this, SIGNAL(sendData(Student*)), messageListUI, SLOT(getData(Student*)));

}

stu_interface::~stu_interface() {
    delete ui;
}



void stu_interface::on_pushButton_2_clicked() {
    QString str = ui->lineEdit->text();
    string s = string((const char *)str.toLocal8Bit());

    int key = ui->comboBox->currentIndex();
    //如果直接把ui->comboBox->currentIndex()放到if里，会无法匹配到合适的选项
    if(key == 0) { //空白
        //模糊查找、显示全部图书，返回列表
        menuList = search_multiplemenuReturn("useFuzzySearch", "name", s, menuDatPath);
        if(menuList.size() == 0) {
            QMessageBox::warning(this, "警告", "没有找到您想要的，请重新输入");
            ui->label_33->show();
            ui->label_34->show();
            ui->label_8->setText(QString(QString::number(0)));
        } else if(menuList.size() > 0) {
            this->showMenu(menuList, 0);
            ui->label_33->show();
            ui->label_34->show();
            ui->label_8->setText(QString(QString::number(menuList.size())));
        }
    } else if(judgeSpace(s) == 0 || s.size() == 0) {
        QMessageBox::warning(this, "warning", "不能输入全空");
        return;
    } else if(key == 1) {
        //按书籍名字查询
        //应当返回一个列表vector<Menu*>

        vector<Menu*>().swap(menuList);

        string searchKey = Qstr2str(str);
//        if (str==NULL)
//            QMessageBox::warning(this,"warning","请输入");
//        else
//        {
        menuList = search_multiplemenuReturn("useFuzzySearch", "name", searchKey, menuDatPath);
        if(menuList.size() == 0) {
            QMessageBox::warning(this, "警告", "没有找到您想要的，请重新输入");
            ui->label_33->show();
            ui->label_34->show();
            ui->label_8->setText(QString(QString::number(0)));
            return ;
        }

        else {
            this->showMenu(menuList, 0);
            ui->label_33->show();
            ui->label_34->show();
            ui->label_8->setText(QString(QString::number(menuList.size())));
        }
//        }



    } else if(key == 2) {
        //按书目ID查询，成功

        FILE *file = openfile(studentDatPath, sizeof(Student)); //打开文件
        fseek(file, 0, SEEK_END);//fseek将文件指针指到文件末尾
        auto offset = ftell(file);//ftell得到文件从指针到文件头的大小（offset是文件总大小）
        int allstunum = offset / sizeof(Student); //确定ID大小
        if(isDigitStr(str) == 0) {
            QMessageBox::warning(this, "warning", "输入错误");
            return ;
        } else {
            int menuID = str.toInt();
            if(menuID > allstunum) {
                QMessageBox::warning(this, "warning", "输入错误，ID过大");
                return;
            } else {

                Menu *menu = search<Menu>(menuID, menuDatPath);
                Menu *temp = new Menu ();
                memset(temp, 0, sizeof (Menu));
                if(memcmp(temp, menu, sizeof (Menu)) != 0) {
                    this->showSingleMenu(menu);
                    ui->label_33->show();
                    ui->label_34->show();
                    ui->label_8->setText(QString(QString::number(1)));
                } else {
                    QMessageBox::warning(this, "警告", "没有找到您想要的，请重新输入");
                    ui->label_33->show();
                    ui->label_34->show();
                    ui->label_8->setText(QString(QString::number(0)));
                }

            }
        }
    } else if(key == 3) {
        //按ISBN号查询,理论上返回唯一menu
        if(judge_menu(s) != 0) {
            QMessageBox::warning(this, "warning", "请输入正确的ISBN，学生界面只支持按照ISBN精确查找");
            return;
        } else {
            Menu* menu = search_onemenuReturn(s, menuDatPath);
            Menu *temp = new Menu ();
            memset(temp, 0, sizeof (Menu));
            if(memcmp(temp, menu, sizeof (Menu)) != 0) {
                this->showSingleMenu(menu);
                ui->label_33->show();
                ui->label_34->show();
                ui->label_8->setText(QString(QString::number(1)));
            } else {
                QMessageBox::warning(this, "警告", "没有找到您想要的，请重新输入");
                ui->label_33->show();
                ui->label_34->show();
                ui->label_8->setText(QString(QString::number(0)));
            }
        }

    } else if(key == 4) {

        //按作者查询，返回vector<Menu*>
        vector<Menu*>().swap(menuList);
        string searchKey = Qstr2str(str);
//        if (str==NULL)
//            QMessageBox::warning(this,"warning","请输入");
//        else
//        {
        menuList = search_multiplemenuReturn("useFuzzySearch", "writer", searchKey, menuDatPath);
        if(menuList.size() == 0) {
            QMessageBox::warning(this, "警告", "没有找到您想要的，请重新输入");
            ui->label_33->show();
            ui->label_34->show();
            ui->label_8->setText(QString(QString::number(0)));
            return ;
        }

        else {
            this->showMenu(menuList, 0);
            ui->label_33->show();
            ui->label_34->show();
            ui->label_8->setText(QString(QString::number(menuList.size())));
        }
    }

//    }
}

void stu_interface::showSingleMenu(Menu *menu) {
    this->show1();
    this->showpage();
    QString key = QString(QString::fromLocal8Bit(menu->name));
    ui->label_9->setText(key);
    key = QString(QString::fromLocal8Bit(menu->ISBN));
    ui->label_10->setText(key);
    key = QString(QString::fromLocal8Bit(menu->writer));
    ui->label_11->setText(key);
    ui->label_8->setText(QString::number(1));
    ui->lineEdit_9->setText(QString::number(1));
}

void stu_interface::showMenu(vector<Menu*>menulist, int numofpage) {
    pagesize = (menulist.size() - 1) / page_size;
    int i = numofpage * page_size;
    QString key;
    this->clear1();
    if(numofpage <= pagesize && numofpage >= 0) {
        if(menulist.size() > i) {
            key = QString(QString::fromLocal8Bit(menulist[i]->name));
            ui->label_9->setText(key);
            key = QString(QString::fromLocal8Bit(menulist[i]->ISBN));
            ui->label_10->setText(key);
            key = QString(QString::fromLocal8Bit(menulist[i]->writer));
            ui->label_11->setText(key);
            this->show1();
            this->showpage();
        }
        if(menulist.size() > i + 1) {
            key = QString(QString::fromLocal8Bit(menulist[i + 1]->name));
            ui->label_12->setText(key);
            key = QString(QString::fromLocal8Bit(menulist[i + 1]->ISBN));
            ui->label_13->setText(key);
            key = QString(QString::fromLocal8Bit(menulist[i + 1]->writer));
            ui->label_14->setText(key);
            this->show2();
        }
        if(menulist.size() > i + 2) {
            key = QString(QString::fromLocal8Bit(menulist[i + 2]->name));
            ui->label_18->setText(key);
            key = QString(QString::fromLocal8Bit(menulist[i + 2]->ISBN));
            ui->label_19->setText(key);
            key = QString(QString::fromLocal8Bit(menulist[i + 2]->writer));
            ui->label_20->setText(key);
            this->show3();
        }
        page = numofpage;
        ui->label_8->setText(QString::number(pagesize + 1));
        ui->lineEdit_9->setText(QString::number(page + 1));
    }
}
//跳转
void stu_interface::on_pushButton_7_clicked() {
    QString str = ui->lineEdit_9->text();
    if(isDigitStr(str) == 0) {
        QMessageBox::warning(this, "warning", "请正确输入");
    } else {
        int jumpofpage = str.toInt();
        if(jumpofpage >= page && jumpofpage <= pagesize)
            this->showMenu(menuList, jumpofpage - 1);
        else {
            QMessageBox::warning(this, "警告", "找不到该页！");
        }
    }
}
//翻页
void stu_interface::on_pushButton_8_clicked() {
    if(page > 0) {
        page--;
        this->showMenu(menuList, page);
    } else if (page == 0) {
        QMessageBox::warning(this, "警告", "已经是第一页！");
    }
}

void stu_interface::on_pushButton_9_clicked() {
    if(page < pagesize) {
        page++;
        this->showMenu(menuList, page);
    } else if(page == pagesize) {
        QMessageBox::warning(this, "警告", "已经是最后一页！");
    }
}

void stu_interface::on_pushButton_clicked() { //重置密码
    //登录界面传参Student*到此页
    //点击打开新界面changeCode，再传参到changeCode，修改密码，提交
    emit sendData(stu);
    changeUI->open();
}

//查看信箱
void stu_interface::on_pushButton_23_clicked() {
    emit sendData(stu);
    messageListUI->open();
}

//写信
void stu_interface::on_pushButton_24_clicked() {
    emit sendData(stu,1);
    addMessageUI->open();
}


//查看详情
void stu_interface::on_pushButton_5_clicked() {
    QString str = ui->label_10->text();
    string ISBN = string((const char *)str.toLocal8Bit());
    Menu *menu = search_onemenuReturn(ISBN, menuDatPath);
    emit sendData(menu, this->stu);
    MenuToBookUI->open();
}

void stu_interface::clear1() {
    //书的信息隐藏
    ui->label_33->hide();
    ui->label_34->hide();
    ui->label_8->clear();
    ui->label_8->hide();
    ui->label_9->clear();
    ui->label_9->hide();
    ui->label_10->clear();
    ui->label_10->hide();
    ui->label_11->clear();
    ui->label_11->hide();
    ui->label_12->clear();
    ui->label_12->hide();
    ui->label_13->clear();
    ui->label_13->hide();
    ui->label_14->clear();
    ui->label_14->hide();
    ui->label_18->clear();
    ui->label_18->hide();
    ui->label_19->clear();
    ui->label_19->hide();
    ui->label_20->clear();
    ui->label_20->hide();
    //查看信息 收藏 隐藏
    ui->pushButton_5->hide();
    ui->pushButton_6->hide();
    ui->pushButton_10->hide();
    ui->pushButton_19->hide();
    ui->pushButton_20->hide();
    ui->pushButton_21->hide();
    //页码信息 隐藏
    ui->lineEdit_9->hide();
    ui->label_8->clear();
    ui->label_8->hide();
    ui->pushButton_7->hide();
    ui->pushButton_8->hide();
    ui->pushButton_9->hide();
}

void stu_interface::showpage() {
    //页面信息显现
    ui->lineEdit_9->show();
    ui->label_8->show();
    ui->pushButton_7->show();
    ui->pushButton_8->show();
    ui->pushButton_9->show();
}

void stu_interface::show1() {
    //第一行信息显现
    ui->pushButton_5->show();
    ui->label_9->show();
    ui->label_10->show();
    ui->label_11->show();
    ui->pushButton_19->show();
}

void stu_interface::show2() {
    //第二行信息
    ui->pushButton_6->show();
    ui->label_12->show();
    ui->label_13->show();
    ui->label_14->show();
    ui->pushButton_20->show();
}

void stu_interface::show3() {
    //第三行信息
    ui->pushButton_10->show();
    ui->label_18->show();
    ui->label_19->show();
    ui->label_20->show();
    ui->pushButton_21->show();
}

void stu_interface::getData(Student *stu, int type) {
    if(type==1)
    {
        clear1();
        clear2();
        clear3();
        clear4();
        ui->lineEdit->clear();
        this->stu = stu;
        QString key = QString(QString::fromLocal8Bit(this->stu->name));
        ui->label_15->setText(key);
        key = QString(QString::fromLocal8Bit(this->stu->stu_id));
        ui->label_16->setText(key);
        key = QString(QString::fromLocal8Bit(this->stu->major));
        ui->label_17->setText(key);
        F5_Record();
        F5_Star();
        F5_Reserve();
    }
}



void stu_interface::on_pushButton_6_clicked() {
    QString str = ui->label_13->text();
    string ISBN = string((const char *)str.toLocal8Bit());
    Menu *menu = search_onemenuReturn(ISBN, menuDatPath);
    emit sendData(menu, this->stu);
    MenuToBookUI->open();
}

void stu_interface::on_pushButton_10_clicked() {
    QString str = ui->label_19->text();
    string ISBN = string((const char *)str.toLocal8Bit());
    Menu *menu = search_onemenuReturn(ISBN, menuDatPath);
    emit sendData(menu, this->stu);
    MenuToBookUI->open();
}

void stu_interface::clear2() {
    ui->label_5->clear();
    ui->label_5->hide();
    ui->label_24->clear();
    ui->label_24->hide();
    ui->label_25->clear();
    ui->label_25->hide();
    ui->label_26->clear();
    ui->label_26->hide();
    ui->label_27->clear();
    ui->label_27->hide();
    ui->label_28->clear();
    ui->label_28->hide();
    ui->label_29->clear();
    ui->label_29->hide();
    ui->label_30->clear();
    ui->label_30->hide();
    ui->label_31->clear();
    ui->label_31->hide();
    ui->label_51->clear();
    ui->label_51->hide();
    ui->label_52->clear();
    ui->label_52->hide();
    ui->label_50->clear();
    ui->label_50->hide();
    ui->pushButton_3->hide();
    ui->pushButton_4->hide();
    ui->pushButton_11->hide();
    ui->pushButton_12->hide();
    ui->pushButton_13->hide();
    ui->pushButton_14->hide();
}

void stu_interface::show21() {
    ui->label_5->show();
    ui->label_24->show();
    ui->label_25->show();
    ui->label_50->show();
    ui->pushButton_3->show();
    ui->pushButton_4->show();
}

void stu_interface::show22() {
    ui->label_26->show();
    ui->label_27->show();
    ui->label_28->show();
    ui->label_51->show();
    ui->pushButton_11->show();
    ui->pushButton_12->show();
}

void stu_interface::show23() {
    ui->label_29->show();
    ui->label_30->show();
    ui->label_31->show();
    ui->label_52->show();
    ui->pushButton_13->show();
    ui->pushButton_14->show();
}

void stu_interface::showRecord() {
    char *name = {0}, *isbn = {0};
    QString key;
    key = QString(QString::number(recordList.size()));
    ui->label_32->setText(key);
    switch (recordList.size()) {
    case 3:
        show23();
        name = find1(recordList[2]->bookId, name);
        key = QString(QString::fromLocal8Bit(name));
        ui->label_29->setText(key);
        isbn = find2(recordList[2]->bookId, isbn);
        key = QString(QString::fromLocal8Bit(isbn));
        ui->label_30->setText(key);
        key = QString(fomatTime(recordList[2]->borrowTime));
        ui->label_31->setText(key);
        key = QString(fomatTime(recordList[2]->latestReturnTime));
        ui->label_52->setText(key);
    case 2:
        show22();
        name = find1(recordList[1]->bookId, name);
        key = QString(QString::fromLocal8Bit(name));
        ui->label_26->setText(key);
        isbn = find2(recordList[1]->bookId, isbn);
        key = QString(QString::fromLocal8Bit(isbn));
        ui->label_27->setText(key);
        key = QString(fomatTime(recordList[1]->borrowTime));
        ui->label_28->setText(key);
        key = QString(fomatTime(recordList[1]->latestReturnTime));
        ui->label_51->setText(key);
    case 1:
        show21();
        name = find1(recordList[0]->bookId, name);
        key = QString(QString::fromLocal8Bit(name));
        ui->label_5->setText(key);
        isbn = find2(recordList[0]->bookId, isbn);
        key = QString(QString::fromLocal8Bit(isbn));
        ui->label_24->setText(key);
        key = QString(fomatTime(recordList[0]->borrowTime));
        ui->label_25->setText(key);
        key = QString(fomatTime(recordList[0]->latestReturnTime));
        ui->label_50->setText(key);
    case 0:
        break;
    default:
        break;
    }
}

char* stu_interface::find1(int bookid, char *name) {
    Book *book = search<Book>(bookid, bookDatPath);
    Menu *menu = search<Menu>(book->menu_ID, menuDatPath);
    name = menu->name;
    return name;
}

char* stu_interface::find2(int bookid, char *isbn) {
    Book *book = search<Book>(bookid, bookDatPath);
    Menu *menu = search<Menu>(book->menu_ID, menuDatPath);
    isbn = menu->ISBN;
    return isbn;
}

void stu_interface::on_pushButton_15_clicked() {
    //刷新
    F5_Record();
}

void stu_interface::on_pushButton_3_clicked() {
    //还书1
    int returnbook = returnBook(recordList[0]->ID);
    if(returnbook == 0) {
        QMessageBox::warning(this, "提示", "还书成功！");
        F5_Record();
    } else if(returnbook == -1) {
        QMessageBox::warning(this, "提示", "逾期还书成功！");
        F5_Record();
    } else if(returnbook == 1) {
        QMessageBox::warning(this, "提示", "还书失败！请联系管理员！");
    }
}


void stu_interface::on_pushButton_11_clicked() {
    //还书2
    int returnbook = returnBook(recordList[1]->ID);
    if(returnbook == 0) {
        QMessageBox::warning(this, "提示", "还书成功！");
        F5_Record();
    } else if(returnbook == -1) {
        QMessageBox::warning(this, "提示", "逾期还书成功！");
        F5_Record();
    } else if(returnbook == 1) {
        QMessageBox::warning(this, "提示", "还书失败！");
    }
}


void stu_interface::on_pushButton_13_clicked() {
    //还书3
    int returnbook = returnBook(recordList[2]->ID);
    if(returnbook == 0) {
        QMessageBox::warning(this, "提示", "还书成功！");
        F5_Record();
    } else if(returnbook == -1) {
        QMessageBox::warning(this, "提示", "逾期还书成功！");
        F5_Record();
    } else if(returnbook == 1) {
        QMessageBox::warning(this, "提示", "还书失败！");
    }
}

void stu_interface::on_pushButton_4_clicked() {
    //续借1
    if(!recordList[0]->isReborrwed) {
        reborrow(recordList[0]->ID);
        QMessageBox::warning(this, "提示", "续借成功！");
        F5_Record();
    } else
        QMessageBox::warning(this, "提示", "您已续借过，续借失败！");
}

void stu_interface::on_pushButton_12_clicked() {
    //续借2
    if(!recordList[1]->isReborrwed) {
        reborrow(recordList[1]->ID);
        QMessageBox::warning(this, "提示", "续借成功！");
        F5_Record();
    } else
        QMessageBox::warning(this, "提示", "您已续借过，续借失败！");
}

void stu_interface::on_pushButton_14_clicked() {
    //续借3
    if(!recordList[2]->isReborrwed) {
        reborrow(recordList[2]->ID);
        QMessageBox::warning(this, "提示", "续借成功！");
        F5_Record();
    } else
        QMessageBox::warning(this, "提示", "您已续借过，续借失败！");
}


void stu_interface::F5_Record() {
    this->clear2();
    this->recordList = search_multiplerecordReturn("notFuzzySearch", "userID", stu->ID, recordDatPath);
    recordList = search_borrowing(recordList);
    showRecord();
}
bool stu_interface::cmp(int x,int y)
{
    return x>y;
}
void stu_interface::showstar() {
    QString key;

    Menu *menu;

    for(int i = 0; i < 3; i++) {
        if(stu->star[i] != -1) {
            menu = search<Menu>(stu->star[i], menuDatPath);
            if(menu->exist == 0)
                stu->star[i] = -1;
        }
    }
    sort(stu->star,stu->star+3);
    int temp=stu->star[0];
    stu->star[0]=stu->star[2];
    stu->star[2]=temp;
    if(stu->star[0] != -1) {
        show31();
        menu = search<Menu>(stu->star[0], menuDatPath);
        key = QString(QString::fromLocal8Bit(menu->name));
        ui->label_40->setText(key);
        key = QString(QString::fromLocal8Bit(menu->ISBN));
        ui->label_41->setText(key);
        key = QString(QString::fromLocal8Bit(menu->writer));
        ui->label_42->setText(key);
        if(stu->star[1] != -1) {
            show32();
            menu = search<Menu>(stu->star[1], menuDatPath);
            key = QString(QString::fromLocal8Bit(menu->name));
            ui->label_43->setText(key);
            key = QString(QString::fromLocal8Bit(menu->ISBN));
            ui->label_44->setText(key);
            key = QString(QString::fromLocal8Bit(menu->writer));
            ui->label_45->setText(key);
            if(stu->star[2] != -1) {
                show33();
                menu = search<Menu>(stu->star[2], menuDatPath);
                key = QString(QString::fromLocal8Bit(menu->name));
                ui->label_46->setText(key);
                key = QString(QString::fromLocal8Bit(menu->ISBN));
                ui->label_47->setText(key);
                key = QString(QString::fromLocal8Bit(menu->writer));
                ui->label_48->setText(key);
            }
        }
    }
}

void stu_interface::show31() {
    ui->label_40->show();
    ui->label_41->show();
    ui->label_42->show();
    ui->pushButton_16->show();
}

void stu_interface::show32() {
    ui->label_43->show();
    ui->label_44->show();
    ui->label_45->show();
    ui->pushButton_17->show();
}

void stu_interface::show33() {
    ui->label_46->show();
    ui->label_47->show();
    ui->label_48->show();
    ui->pushButton_18->show();
}

void stu_interface::F5_Star() {
    clear3();
    this->stu = search<Student>(stu->ID, studentDatPath);
    showstar();
}

void stu_interface::on_pushButton_16_clicked() {
    //取消收藏1
    QString str = ui->label_41->text();
    string isbn = string((const char *)str.toLocal8Bit());
    int key = delstar(stu->ID, isbn);
    if(key == 1) {
        QMessageBox::warning(this, "提示", "取消收藏成功！");
        F5_Star();
    } else if(key == 0) {
        QMessageBox::warning(this, "提示", "取消收藏失败！");
    }
}

void stu_interface::on_pushButton_17_clicked() {
    //取消收藏2
    QString str = ui->label_44->text();
    string isbn = string((const char *)str.toLocal8Bit());
    int key = delstar(stu->ID, isbn);
    if(key == 1) {
        QMessageBox::warning(this, "提示", "取消收藏成功！");
        F5_Star();
    } else if(key == 0) {
        QMessageBox::warning(this, "提示", "取消收藏失败！");
    }
}

void stu_interface::on_pushButton_18_clicked() {
    //取消收藏3
    QString str = ui->label_47->text();
    string isbn = string((const char *)str.toLocal8Bit());
    int key = delstar(stu->ID, isbn);
    if(key == 1) {
        QMessageBox::warning(this, "提示", "取消收藏成功！");
        F5_Star();
    } else if(key == 0) {
        QMessageBox::warning(this, "提示", "取消收藏失败！");
    }
}

void stu_interface::clear3() {
    ui->label_40->clear();
    ui->label_40->hide();
    ui->label_41->clear();
    ui->label_41->hide();
    ui->label_42->clear();
    ui->label_42->hide();
    ui->label_43->clear();
    ui->label_43->hide();
    ui->label_44->clear();
    ui->label_44->hide();
    ui->label_45->clear();
    ui->label_45->hide();
    ui->label_46->clear();
    ui->label_46->hide();
    ui->label_47->clear();
    ui->label_47->hide();
    ui->label_48->clear();
    ui->label_48->hide();
    ui->pushButton_16->hide();
    ui->pushButton_17->hide();
    ui->pushButton_18->hide();
}
void stu_interface::on_pushButton_19_clicked() {
    //收藏1
    QString str = ui->label_10->text();
    string isbn = string((const char *)str.toLocal8Bit());
    Menu *menu = search_onemenuReturn(isbn, menuDatPath);
    int key = addstar(stu->ID, menu);
    if(key == -1) {
        QMessageBox::warning(this, "提示", "您已收藏过该书！");
    } else if(key == -2) {
        QMessageBox::warning(this, "提示", "收藏失败！");
    } else if(key < MAX_STAR && key >= 0) {
        stu->star[key] = menu->ID;
        QMessageBox::warning(this, "提示", "收藏成功！");
    }
}


void stu_interface::on_pushButton_20_clicked() {
    //收藏2
    QString str = ui->label_13->text();
    string isbn = string((const char *)str.toLocal8Bit());
    Menu *menu = search_onemenuReturn(isbn, menuDatPath);
    int key = addstar(stu->ID, menu);
    if(key == -1) {
        QMessageBox::warning(this, "提示", "您已收藏过该书！");
    } else if(key == -2) {
        QMessageBox::warning(this, "提示", "收藏失败！");
    } else if(key < MAX_STAR && key >= 0) {
        stu->star[key] = menu->ID;
        QMessageBox::warning(this, "提示", "收藏成功！");
    }
}

void stu_interface::on_pushButton_21_clicked() {
    //收藏3
    QString str = ui->label_19->text();
    string isbn = string((const char *)str.toLocal8Bit());
    Menu *menu = search_onemenuReturn(isbn, menuDatPath);
    int key = addstar(stu->ID, menu);
    if(key == -1) {
        QMessageBox::warning(this, "提示", "您已收藏过该书！");
    } else if(key == -2) {
        QMessageBox::warning(this, "提示", "收藏失败！");
    } else if(key < MAX_STAR && key >= 0) {
        stu->star[key] = menu->ID;
        QMessageBox::warning(this, "提示", "收藏成功！");
    }
}

void stu_interface::on_pushButton_22_clicked() {
    F5_Star();
}
void stu_interface::clear4() {
    ui->label_194->clear();
    ui->label_194->hide();
    ui->label_195->clear();
    ui->label_195->hide();
    ui->label_196->clear();
    ui->label_196->hide();
    ui->label_197->clear();
    ui->label_197->hide();
    ui->label_198->clear();
    ui->label_198->hide();
    ui->label_199->clear();
    ui->label_199->hide();
    ui->label_200->clear();
    ui->label_200->hide();
    ui->label_201->clear();
    ui->label_201->hide();
    ui->label_202->clear();
    ui->label_202->hide();
    ui->pushButton_86->hide();
    ui->pushButton_87->hide();
    ui->pushButton_88->hide();
}

void stu_interface::show41() {
    ui->label_194->show();
    ui->label_195->show();
    ui->label_196->show();
    ui->pushButton_86->show();
}

void stu_interface::show42() {
    ui->label_197->show();
    ui->label_198->show();
    ui->label_199->show();
    ui->pushButton_87->show();
}

void stu_interface::show43() {
    ui->label_200->show();
    ui->label_201->show();
    ui->label_202->show();
    ui->pushButton_88->show();
}

void stu_interface::showReserve() {
    QString key;

    Menu *menu;
    Book *book;
    sortReserve();
    if(stu->reserve[0] != -1) {
        show41();
        book = search<Book>(stu->reserve[0], bookDatPath);
        menu = search<Menu>(book->menu_ID, menuDatPath);
        key = QString(QString::fromLocal8Bit(menu->name));
        ui->label_194->setText(key);
        key = QString(QString::fromLocal8Bit(menu->ISBN));
        ui->label_195->setText(key);
        key = QString(QString::fromLocal8Bit(menu->writer));
        ui->label_196->setText(key);
        if(stu->reserve[1] != -1) {
            show42();
            book = search<Book>(stu->reserve[1], bookDatPath);
            menu = search<Menu>(book->menu_ID, menuDatPath);
            key = QString(QString::fromLocal8Bit(menu->name));
            ui->label_197->setText(key);
            key = QString(QString::fromLocal8Bit(menu->ISBN));
            ui->label_198->setText(key);
            key = QString(QString::fromLocal8Bit(menu->writer));
            ui->label_199->setText(key);
            if(stu->reserve[2] != -1) {
                show43();
                book = search<Book>(stu->reserve[2], bookDatPath);
                menu = search<Menu>(book->menu_ID, menuDatPath);
                key = QString(QString::fromLocal8Bit(menu->name));
                ui->label_200->setText(key);
                key = QString(QString::fromLocal8Bit(menu->ISBN));
                ui->label_201->setText(key);
                key = QString(QString::fromLocal8Bit(menu->writer));
                ui->label_202->setText(key);
            }
        }
    }
}

void stu_interface::on_pushButton_86_clicked() {
    int bookid = stu->reserve[0];
    int key = cancelReserve(stu->ID, bookid);
    if(key == 0) {
        QMessageBox::warning(this, "提示", "取消预约成功！");
        F5_Reserve();
    } else {
        QMessageBox::warning(this, "提示", "取消预约失败！");
    }
}

void stu_interface::on_pushButton_87_clicked() {
    int bookid = stu->reserve[1];
    int key = cancelReserve(stu->ID, bookid);
    if(key == 0) {
        QMessageBox::warning(this, "提示", "取消预约成功！");
        F5_Reserve();
    } else {
        QMessageBox::warning(this, "提示", "取消预约失败！");
    }
}

void stu_interface::on_pushButton_88_clicked() {
    int bookid = stu->reserve[2];
    int key = cancelReserve(stu->ID, bookid);
    if(key == 0) {
        QMessageBox::warning(this, "提示", "取消预约成功！");
        F5_Reserve();
    } else {
        QMessageBox::warning(this, "提示", "取消预约失败！");
    }
}

void stu_interface::F5_Reserve() {
    clear4();
    this->stu = search<Student>(stu->ID, studentDatPath);
    showReserve();
}

void stu_interface::on_pushButton_89_clicked() {
    F5_Reserve();
}

void stu_interface::sortReserve() {
    for(int i = 0; i < MAX_RESERVE - 1; i++) {
        for(int j = 0; j < MAX_RESERVE - 1 - i; j++) {
            if(stu->reserve[j] < stu->reserve[j + 1]) {
                int t = stu->reserve[j];
                stu->reserve[j] = stu->reserve[j + 1];
                stu->reserve[j + 1] = t;
            }
        }
    }
}
